package com.njupt.swg.threadstudyjimin.item07;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CyclicBarrierTest2 {
    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(5,() -> {
        log.info("callback is running...");
    });

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i=0;i<10;i++){
            Thread.sleep(1000 );
            exec.execute(() -> {
                try{
                    race();
                }catch (Exception e){
                    log.error("exception",e);
                }
            });
        }
        exec.shutdown();
    }

    private static void race() throws InterruptedException {
        Thread.sleep(1000);
        log.info("ready...");
        try {
            cyclicBarrier.await();
        } catch (Exception e) {
            log.warn("exception",e);
        }
        log.info("continue...");
    }
}
